package com.codeczx.videoviewdemo;

import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.content.res.TypedArray;
import android.support.annotation.AttrRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutCompat;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import tv.danmaku.ijk.media.player.IjkMediaPlayer;

/**
 * Created by codeczx on 2017/8/14.
 */

public class CzxVideoView extends FrameLayout {

    private static final String TAG = "PersistVideoView";
    private Context mContext;
    private MediaController mMediaController;
    private IjkMediaPlayer mIjkPlayer;
    private ViewGroup.LayoutParams mLayoutParams;
    private int width;
    private int height;

    private Activity mActivity;


    public CzxVideoView(@NonNull Context context) {
        this(context, null);
    }

    public CzxVideoView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CzxVideoView(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        mActivity = (Activity) mContext;
        initView();
    }

    private void initView() {
        View view = View.inflate(mContext, R.layout.czx_video_view, this);
        mMediaController = new MediaController(mContext);
        addView(mMediaController);
        mMediaController.setVideoController(mVideoController);
        SurfaceView surfaceView = (SurfaceView) view.findViewById(R.id.surface_view);
    }


    public void changeOrientation() {
        AppCompatActivity activity = (AppCompatActivity) mContext;
        int orientation = activity.getRequestedOrientation();
        // The default value of orientation  is SCREEN_ORIENTATION_UNSPECIFIED.
        if (orientation == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
            activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            mLayoutParams.width = width;
            mLayoutParams.height = height;
            setLayoutParams(mLayoutParams);
            mActivity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        } else {
            activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
            mLayoutParams = getLayoutParams();
            width = mLayoutParams.width;
            height = mLayoutParams.height;
            mLayoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
            mLayoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT;
            setLayoutParams(mLayoutParams);
            mActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        }
    }

    public interface VideoController {
        void changeViewSize();
    }

    private VideoController mVideoController = new VideoController() {
        @Override
        public void changeViewSize() {
            changeOrientation();
        }
    };


}
